<?php
/**
 * @version 1.2 
 * add mothid: GSomeFiledsRAr
 */
class mysql {
	private $host = ''; //
	private $name = ''; //
	private $pwd = ''; //
	private $dBase = ''; //
	private $conn = ''; //
	private $result = ''; //
	private $msg = ''; //	
	private $fields; //
	private $fieldsNum = 0; //
	private $rowsNum = 0; //
	private $rowsRst = ''; //
	private $filesArray = array (); //
	private $rowsArray = array ();
	private $sql = '';
	private $_z = '';
	
	static function init() {
		return new self ();
	}
	/**
	 * 
	 *
	 * @param string $host
	 * @param string $name
	 * @param string $pwd
	 * @param string $dBase
	 */
	
	function __construct($db_config) {
		
		global $z;
		if ($db_config ["host"] != '')
			$this->host = $db_config ["host"];
		if ($db_config ["username"] != '')
			$this->name = $db_config ["username"];
		if ($db_config ["password"] != '')
			$this->pwd = $db_config ["password"];
		if ($db_config ["dbname"] != '')
			$this->dBase = $db_config ["dbname"];
		if ($db_config != null)
			$this->init_conn ();
		
		if (! is_object ( $z )) {
			$dir = dirname ( __FILE__ ) . '/z.php';
			$a = require_once $dir;
			$this->_z = $z;
		} else {
			$this->_z = $z;
		}
	
	}
	
	function serchindb($key, $path) {
		$tbsar = $this->getDbTableName ();
		foreach ( $tbsar as $k => $v ) {
			$db = $this->dBase;
			$tbname = $v ["Tables_in_$db"];
			$fieldar = $this->gettbFields ( $tbname );
			$where = 'where ';
			foreach ( $fieldar as $tbnamek => $tbnamev ) {
				$where .= " $tbnamev like '%$key%' or";
			}
			$where = substr ( $where, 0, - 3 );
			$sql = "SELECT * from $tbname $where";
			$rowar = array ();
			if ($rowar = $this->row_getall ( $sql )) {
				foreach ( $rowar as $rowk => $rowv ) {
					$rowv ['table'] = $tbname;
					$this->_z->wlog ( $path . "/serch$key", $rowv );
				}
			}
		}
	
	}
	
	/**
	 * init this class
	 *
	 */
	function init_conn() {
		$this->conn = @mysql_connect ( $this->host, $this->name, $this->pwd );
		@mysql_select_db ( $this->dBase, $this->conn );
		mysql_query ( "set names utf8" );
	}
	/**
	 * 
	 *
	 * @param string $sql
	 * @return obj result
	 */
	function query($sql) {
		
		if ($this->conn == '') {
			$this->init_conn ();
		}
		$this->result = @mysql_query ( $sql, $this->conn );
		return $this->result;
	}
	function insert_id() {
		return @mysql_insert_id ( $this->conn );
	}
	/**
	 * get all the table from this db
	 *
	 * @return array array()
	 */
	function getDbTableName() {
		$this->sql = "SHOW TABLES FROM " . $this->dBase;
		$result = mysql_query ( $this->sql );
		while ( $row = mysql_fetch_assoc ( $result ) ) {
			$tempar [] = $row;
		}
		
		return $tempar;
	}
	
	function gettbFields($tbname) {
		$sql = "SELECT   *   FROM   $tbname";
		$this->result = @mysql_query ( $sql, $this->conn );
		
		$i = 0;
		$Field_Name = array ();
		while ( $i < mysql_num_fields ( $this->result ) ) {
			$Field_Name [$i] = mysql_field_name ( $this->result, $i );
			$i ++;
		}
		return $Field_Name;
	}
	
	function row_nums($sql = '') {
		if ($sql != '')
			$this->result = $this->query ( $sql );
		if ($this->result) {
			return @mysql_num_rows ( $this->result );
		} else {
			return '';
		}
	}
	
	/**
	 * 
	 * @param string table
	 * @return array() onle one
	 */
	function GTFirst($t) {
		$this->sql = "SELECT * FROM  $t LIMIT 0,1";
		$this->result = mysql_query ( $this->sql );
		return mysql_fetch_assoc ( $this->result );
	}
	
	/**
	 * 
	 * @param $tbname
	 * @param $row  must field => value
	 */
	function row_insert($tbname, $row) {
		$sqlfield = '';
		$sqlvalue = '';
		foreach ( $row as $key => $value ) {
			$sqlfield .= $key . ',';
			$sqlvalue .= "'" . $value . "',";
		}
		$sql = "INSERT INTO `" . $tbname . "`(" . substr ( $sqlfield, 0, - 1 ) . ") VALUES (" . substr ( $sqlvalue, 0, - 1 ) . ")";
		
		return $this->query ( $sql );
	}
	
	/**
	 * Enter description here...
	 *
	 * @param string $t
	 * @param array $fields
	 * @param array $values
	 * @return int records
	 * @example $ZDB->_Insert('room_list',array('room_num','subject'),array('ese','dfsd'));
	 * 
	 */
	//
	function row_update($tbname, $row, $where) {
		foreach ( $row as $key => $value ) {
			$sqlud .= $key . "= '" . $value . "',";
		}
		$sql = "UPDATE `" . $tbname . "` SET " . substr ( $sqlud, 0, - 1 ) . " WHERE " . $where;
		return $this->query ( $sql );
	}
	/**
	 * Enter description here...
	 *
	 * @param string $t
	 * @param string $where
	 * @return int records
	 * @example $d=$ZDB->_Delete('room_list',"id=$Id");
	 * 
	 * 
	 */
	function row_Delete($t, $where) {
		$this->sql = 'DELETE FROM ' . $t . ' where ' . $where . '';
		return mysql_query ( $this->sql );
	}
	
	/**
	 * 
	 * @param $sql
	 */
	function row_getall($sql) {
		$result = $this->query ( $sql );
		if ($result) {
			$rowar = array ();
			while ( $row = mysql_fetch_array ( $result, MYSQL_ASSOC ) ) {
				$rowar [] = $row;
			}
			return $rowar;
		} else
			return false;
	
	}
	
	function row_getone($sql) {
		$result = $this->query ( $sql );
		if ($result) {
			$row = mysql_fetch_array ( $result, MYSQL_ASSOC );
			return $row;
		} else
			return false;
	}
	/**
	 * 
	 *
	 * @param string $sql
	 * @return array or ''
	 */
	function row_select($tbname, $where = "", $limit = 0, $fields = "*", $orderby = "id", $sort = "DESC") {
		$sql = "SELECT " . $fields . " FROM `" . $tbname . "` " . ($where ? " WHERE " . $where : "") . " ORDER BY " . $orderby . " " . $sort . ($limit ? " limit " . $limit : "");
		$temp = $this->query ( $sql );
		if ($temp) {
			while ( $row = mysql_fetch_array ( $temp, MYSQL_ASSOC ) ) {
				$tempar [] = $row;
			}
			return $tempar;
		} else
			return false;
	
	}
	
	function row_select_one($tbname, $where, $fields = "*") {
		$sql = "SELECT " . $fields . " FROM `" . $tbname . "` " . ($where ? " WHERE " . $where : "");

		$temp = $this->query ( $sql );
		
		if ($temp) {
			return mysql_fetch_assoc ( $temp );
		} else
			return false;
	}
	
	/**
	 * 
	 * 
	 *
	 * @return error
	 */
	function msg_error() {
		if (mysql_errno () != 0) {
			$this->msg = mysql_error ();
		}
		return $this->msg;
	}
	
	/**
	 * @uses  defind null
	 *
	 */
	function FreeVar() {
		//mysql_free_result($this->result);
		$this->msg = '';
		$this->fieldsNum = 0;
		$this->rowsNum = 0;
		$this->filesArray = '';
		$this->rowsArray = '';
	}
	
	/**
	 * @uses close connid
	 *
	 */
	function close_conn() {
		$this->FreeVar ();
		mysql_close ( $this->conn );
		$this->conn = '';
	}
}
$mysql = new mysql($db_config);